﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Business.Shop.Commands.Order;
using Newtonsoft.Json;
using ZLCMS.Web.SystemManage.Basics.ProductSKU;
using ZLCMS.Web.Wap.Code;
using ZLCMS.Business.Shop.Data;


namespace ZLCMS.Web.MemberUser
{
    public partial class ViewOrder : WebMemberPageBase
    {
        public MemberInfo memberInfo;
        public ShopOrderInfo _order;
        public string _orderNo;
        public ShopShippingInfo shippingInfo;
        public ShopPaymentInfo paymentInfo;

        protected void Page_Load(object sender, EventArgs e)
        {
            _orderNo = Request["orderno"] == null ? "" : Request["orderno"];
            string _opt = Request["opt"] == null ? "" : Request["opt"];

            InitGlobalValues();



            rptOrderProBind();
        }

        #region 初始化全局变量 + private void InitGlobalValues()
        /// <summary>
        /// 初始化全局变量
        /// </summary>
        private void InitGlobalValues()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _order = session.CreateCriteria<ShopOrderInfo>().Add(Expression.Eq("OrderNo", _orderNo)).UniqueResult<ShopOrderInfo>();
                    shippingInfo = session.CreateCriteria<ShopShippingInfo>().Add(Expression.Eq("Id", int.Parse(_order.ShippingType))).UniqueResult<ShopShippingInfo>();
                    paymentInfo = session.CreateCriteria<ShopPaymentInfo>().Add(Expression.Eq("Id", int.Parse(_order.PaymentType))).UniqueResult<ShopPaymentInfo>();
                    if (_order == null)
                    {
                        throw new Exception("查询不到相关订单!");
                    }
                }
                catch (Exception ex)
                {
                    Response.Write("查询出错!" + ex.Message);
                    Response.Redirect("/MemberUser/MyOrder.aspx");
                }
            }
        }
        #endregion

        #region 绑定订单商品信息 + private void rptOrderProBind()
        /// <summary>
        /// 绑定订单商品信息
        /// </summary>
        private void rptOrderProBind()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {

                    LoadOrderDetailResult res = (LoadOrderDetailResult)new LoadOrderDetailCommand().Execute(new LoadOrderDetailParameter() { OrderNo = _order.OrderNo });

                    rptOrderPro.DataSource = res.Result;
                    rptOrderPro.DataBind();

                }
                catch (Exception ex)
                {
                    Response.Write("查询出错!" + ex.Message);
                }
            }
        }
        #endregion

        public string GetSkuItemHtml(string skuProductNo)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ShopSkuProduct product = session.CreateQuery("from ShopSkuProduct where ProductSkuCode=:code ").SetParameter("code", skuProductNo).UniqueResult<ShopSkuProduct>();

                List<SKUGroup> grouplist = JsonConvert.DeserializeObject<List<SKUGroup>>(product.ParamCode);
                string itemList = "";
                foreach (SKUGroup cd in grouplist)
                {
                    itemList += "<span>[" + cd.ItemName + "]</span> ";
                }

                return itemList;
            }
        }
    }
}